package com.nit.tools;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * 时间工具类
 *
 * @Auth: 逆天开源-逍遥哥哥
 * @link 1715656022@qq.com
 * @Date: 2023-07-11
 */
@Slf4j
public class NitDateTimeUtils {
    public static final String TIME_FORMATTER = "HH:mm:ss";

    public static void main(String[] args) {
        // 获取当前时间
        System.out.println(NitDateTimeUtils.getNowTimeStr());
        System.out.println(NitDateTimeUtils.getCurrentTimeStr(LocalTime.now()));
        // 定义两个时间字符串
        String timeStr1 = "12:30:45";
        String timeStr2 = "11:20:00";
        String timeStr3 = "22:01:00";
//        System.out.println(NitDateTimeUtils.compare(timeStr1, timeStr2));
        System.out.println(NitDateTimeUtils.compareTo(timeStr1, timeStr2));

        System.out.println(NitDateTimeUtils.timeIsInRound(timeStr1, timeStr2, timeStr3));
        System.out.println(NitDateTimeUtils.timeIsInRound(timeStr2, timeStr1, timeStr3));
        System.out.println(NitDateTimeUtils.timeIsInRound(timeStr3, timeStr2, timeStr1));
        LocalTime lt = LocalTime.now();
        System.out.println(lt);//12:34:03.506412100

        System.out.println("今天剩余毫秒数：" + NitDateTimeUtils.getRemainingMilliseconds() / 1000 / 60 / 60);
    }

    /**
     * timeStr1 > timeStr2 返回  1
     * timeStr1 == timeStr2 返回  0
     * timeStr1 < timeStr2 返回  -1
     *
     * @param timeStr1 HH:mm:ss
     * @param timeStr2 HH:mm:ss
     * @param formatterStr  时间格式定义
     * @return R int
     */
    public static int compare(String timeStr1, String timeStr2,String formatterStr) {
        if (timeStr1.equals(timeStr2)) {
            return 0;
        }
        // 定义时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatterStr);
        // 将时间字符串转换为LocalTime对象
        LocalTime time1Obj = LocalTime.parse(timeStr1, formatter);
        LocalTime time2Obj = LocalTime.parse(timeStr2, formatter);

        // 比较两个时间对象
        if (time1Obj.isBefore(time2Obj)) {
            return -1;
        } else if (time1Obj.isAfter(time2Obj)) {
            return 1;
        }
        return 0;
    }


    /**
     * 与compareTo 功能一样楼
     *
     * @param timeStr1 时间1 HH:mm:ss
     * @param timeStr2 时间2 HH:mm:ss
     * @return R int
     */
    public static int compareTo(String timeStr1, String timeStr2) {
        LocalTime lt = LocalTime.parse(timeStr1);
        LocalTime lt2 = LocalTime.parse(timeStr2);
        int timeFlag = lt.compareTo(lt2);
        return timeFlag;
    }
    public static int compareToByTimeFormatter(String timeStr1, String timeStr2,String formatterStr) {
        LocalTime lt = LocalTime.parse(timeStr1);
        LocalTime lt2 = LocalTime.parse(timeStr2);
        int timeFlag = lt.compareTo(lt2);
        return timeFlag;
    }

    /**
     * 包含等于
     * 比较一个 HH:mm:ss 是否在一个时间段内
     * 如：14:33:00 是否在 09:30:00 和 12:00:00 内
     */
    public static boolean timeIsInRound(String str1, String start, String end) {
        if (str1.equals(start) || str1.equals(end)) {
            return true;
        }
        return 1 == compareTo(str1, start) && -1 == compareTo(str1, end);
    }


    /**
     * @return R 获取当前时分秒 HH:mm:ss
     */
    public static String getNowTimeStr() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(TIME_FORMATTER);
        return LocalTime.now().format(formatter);
    }

    /**
     * 获取指定LocalTime 的时分秒
     *
     * @param current 指定LocalTime
     * @return R HH:mm:ss
     */
    public static String getCurrentTimeStr(LocalTime current) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(TIME_FORMATTER);
        return current.format(formatter);
    }

    /**
     * 获取今日剩余毫秒数
     *
     * @return
     */
    public static long getRemainingMilliseconds() {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        // 获取明天的零点时
        LocalDateTime tomorrowMidnight = now.plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        // 计算今天剩余的毫秒数
        long remainingMilliseconds = ChronoUnit.MILLIS.between(now, tomorrowMidnight);
        return remainingMilliseconds;
    }
}
